// 如果没登录显示提示登录信息
if (selectCookie("keyusername")) {
    $('.empty-shop h2').next().remove()
    $('.login-btn').remove()
}else{
    $('.empty-shop').show()
    $('.have_shop').hide()
}

$('.h_logo').click(function(){
    location.href="./home.html"
})

// 判断如果购物车有东西就显示购物车没有就显示提示购买
var username=selectCookie("keyusername")

var str = localStorage.getItem('cartdata')
if(str){
    var arr = JSON.parse(str)
    arr = arr.filter(item=>item.username === username)  
    if(arr.length){
        var ids = arr.map(item=>item.pid).concat()
        // console.log(ids);
        $.ajax({
            url:"./data/list.json",
            dataType:'json',
            type:'GET',
            success:res=>{
               for(var i=0;i<res.length;i++){
                   for(var j=0;j<ids.length;j++){
                        if(res[i].pid==ids[j]){
                            // console.log(res[i]);

                            var str='<tr><td>'
                            str+=` <input type="checkbox"class="selectOne">
                            &nbsp;&nbsp; 选择</td><td><img src=${res[i].imgsrc} alt=""></td>
                            <td align="left">${res[i].name}</td>
                            <td class="price">${res[i].price}</td><td align="center"><div class="jsq"> <a href="javascript:;" class="reduce">—</a><span class="number">${arr[j].number}</span><a href="javascript:;" class="add">+</a></div></td><td class="subtotal">${arr[j].number*res[i].price}</td><td data-id=${res[i].pid}><i class="iconfont icon-chahao remove"></i></td>
                            </tr>`

                            $('tbody').prepend($(str))
                        }
                   }     
               }
               selectAll()
               selectOne()
               reduce()
               add()
               subtotal()
               all()
               removetr()
               tip()
            }
        })



    }

}

function removetr(){
    $('.remove').click(function(){
        $(this).parent().parent().remove()

        var pid=$(this).parent().attr('data-id')
        // console.log(pid);
        var data=JSON.parse(localStorage.getItem('cartdata'))
        var index=data.findIndex(item=>item.pid == pid && item.username === username);
        data.splice(index,1)
        localStorage.setItem('cartdata',JSON.stringify(data))
        tip()
    })
}

function selectAll(){
    $('.selectAll').click(function(){
        $('.selectOne').prop('checked',$('.selectOne').prop('checked',true))
        $('.selectAll').prop('checked',$('.selectOne').prop('checked',true))
        all()
    })
  
}
function selectOne(){
    $('.selectOne').click(function(){
        var arr = Array.prototype.slice.call($('.selectOne'))
        // 调用every方法
        var bool = arr.every(item=>item.checked)
        $('.selectAll').prop('checked',bool)
        all()
    })
 
    
}

function reduce(){
    $('a.reduce').click(function(){
        
        var num=$(this).next().text()-0
        num--
        if(num<=1){
            num=1
        }
        $(this).next().text(num)  
        
        var data = JSON.parse(localStorage.getItem('cartdata'))
        var pid = $(this).parent().parent().siblings().last().attr('data-id')
        var currentData = data.find(item=>item.username === username && item.pid == pid)        
        currentData.number=num
        localStorage.setItem('cartdata',JSON.stringify(data))
        subtotal()
        all()
    })
}
function add(){
    $('a.add').click(function(){
        
        var num=$(this).prev().text()-0
        num++
        
        $(this).prev().text(num)    
        
        var data = JSON.parse(localStorage.getItem('cartdata'))
        var pid = $(this).parent().parent().siblings().last().attr('data-id')
        var currentData = data.find(item=>item.username === username && item.pid == pid)        
        currentData.number=num
        localStorage.setItem('cartdata',JSON.stringify(data))
        subtotal()
        all()
    })
}
// 小计
function subtotal(){
    $('tbody .price').each((index,item)=>{
        var price = $(item).text() - 0;
        var number =  $(item).next().children().find('.number').text() - 0;
        var sub = price * number;
        $(item).siblings('.subtotal').text(sub)
    })
}
function all(){
    var total=0
    $('.subnum').text($('.selectOne').length)
    $('.selectnum').text($('.selectOne:checked').length)
    $('.selectOne:checked').each(function(index,item){
        total+= $(item).parent().siblings('.subtotal').text()-0
    })
    
    $('.total').text(total)
}

function tip(){
    var arr=JSON.parse(localStorage.getItem('cartdata'))
    var data = arr.filter(item=>item.username === username)
    console.log(1);
    if(!data.length){
        $('.empty-shop').show()
        $('.have_shop').hide()
    }
}
tip()

$.ajax({
    url:"./data/list.json",
    dataType:'json',
    type:'GET',
    success:res=>{
        var str=''
        res.forEach(item => {
            str+=`<li>
            <a href="javascript:;" class="forcont">
                <img src=${item.listimg}>
                <p class="desc">
                    ${item.name}
                </p>
                <p class="comprice">${item.price}</p>
                <p class="re-tip">10万人好评</p>
            </a>
            <a href="./Details.html?pid=${item.pid}" class="addcart">
                加入购物车
            </a>
        </li>`
        });
        $('.foryouproduct>ul').append($(str))
    }
})
